package com.weme.live.dialog

import android.animation.ValueAnimator
import android.content.Context
import android.graphics.Color
import android.os.Bundle
import android.os.CountDownTimer
import android.view.*
import androidx.core.content.ContextCompat
import androidx.fragment.app.FragmentActivity
import com.google.android.material.bottomsheet.BottomSheetBehavior
import com.google.android.material.bottomsheet.BottomSheetDialog
import com.gyf.immersionbar.BarHide
import com.gyf.immersionbar.ImmersionBar
import com.weme.common.dialog.RechargeDialog
import com.weme.common.dialog.RechargeDialogCallback
import com.weme.common.network.RECHARGE_GUIDE
import com.weme.common.util.*
import com.weme.live.databinding.DialogLivePayBinding
import dev.kratos.api.user_server.v1.GiftInfo

class LivePayDialog(var mContext: FragmentActivity, val isRecharge: Boolean) : BottomSheetDialog(mContext, com.weme.common.R.style.TUIBeautyDialogTheme) {


    private var countdownAnimator: ValueAnimator? = null
    private lateinit var binding: DialogLivePayBinding
    private lateinit var countDownTimer: CountDownTimer


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = DialogLivePayBinding.inflate(LayoutInflater.from(context), null, false) //   giftAdapter = GiftAdapter(requireContext(),listOf("", "", "", "", ""))
        setContentView(binding.root)
        ImmersionBar.with(mContext).hideBar(BarHide.FLAG_HIDE_NAVIGATION_BAR).init()
        countdownAnimator = binding.lrbTime.startCountdown(maxProgress = 25, // 倒计时总时长（秒）
            onProgressUpdate = { progress -> //  binding.tvTime.text = String.format(progress.toInt().toString())
                binding.lrbTime.setProgress(progress)

            }, onComplete = {})

        countDownTimer = object : CountDownTimer(25000, 1000) { // 25秒，每秒执行一次
            override fun onTick(millisUntilFinished: Long) {
                val secondsRemaining = millisUntilFinished / 1000
                binding.tvTime.text = secondsRemaining.toString()
            }

            override fun onFinish() {
                binding.tvTime.text = "0"
            }
        }
        countDownTimer.start()
        if (isRecharge) {
            binding.smCalView.rectColor = Color.parseColor("#C616A6")
            binding.smCalView.endColor = Color.parseColor("#8C17BB")
            binding.imCoin.visible()
            binding.tvContent.text = AppMkHelper.decodeString(RECHARGE_GUIDE)
            binding.tvCoins.text = context.getString(com.example.resources.R.string.coins)
            binding.imVip.gone()
            binding.tvCoins.visible()
        } else {
            binding.smCalView.rectColor = ContextCompat.getColor(context, com.example.resources.R.color.app_btn_colors)
            binding.imCoin.gone()
            binding.imVip.visible()
            binding.tvCoins.gone()
            binding.tvContent.text = "1"
            binding.tvCoins.text = context.getString(com.example.resources.R.string.live_pay_vip)
        }

        binding.smTop.setOnClickListener {
            if (isRecharge) {
                try {
                    RechargeDialog.show(context, object : RechargeDialogCallback {
                        override fun sendGiftInfo(giftInfo: GiftInfo) {

                        }

                    })
                } catch (e: Exception) {

                }

            } else {
                context.showToast("跳转到订阅商城")
            }
        }

    }

    override fun onBackPressed() {
        val behavior = BottomSheetBehavior.from(binding.root.parent as View)
        if (behavior.state != BottomSheetBehavior.STATE_COLLAPSED) {
            behavior.state = BottomSheetBehavior.STATE_COLLAPSED
        } else {
            super.onBackPressed()
        }
    }

    override fun onTouchEvent(event: MotionEvent): Boolean {
        // 覆盖此方法以防止对话框在点击外部区域时关闭
        if (isShowing) {
            // 判断点击是否在对话框外部
            val x = event.x.toInt()
            val y = event.y.toInt()
            val content = findViewById<View>(com.google.android.material.R.id.design_bottom_sheet)
            if (content != null && (x < content.left || x > content.right || y < content.top || y > content.bottom)) {
                return true
            }
        }
        return super.onTouchEvent(event)
    }

    override fun onDetachedFromWindow() {
        countdownAnimator?.cancel()
        countdownAnimator?.clone()
        countDownTimer.cancel()
        countdownAnimator = null
        super.onDetachedFromWindow()
    }


}